/**
 * @file LinesTool.cpp
 *
 * @date 18/12/2011
 * @author Tales 
 *
 */

#include "LinesTool.hpp"
//#include <SDL_gfxPrimitives.h>
#include "Editor.hpp"
#include "../common/util.hpp"
#include "../model/Picture.hpp"
#include "../model/DrawLinesCommand.hpp"
#include "../Graphics.hpp"

namespace yep
{

LinesTool::LinesTool()
{
}

LinesTool::~LinesTool()
{
}

void LinesTool::start()
{
	drawing = false;
	Editor &editor = getEditor();
	SDL_FillRect(editor.getPreview(), NULL, 0x0);
}

void LinesTool::end()
{
	vertices.clear();
}

void LinesTool::pressed(pair<int, int> pos, bool leftButton, bitset<5> state)
{
	if (leftButton && !drawing)
	{
		drawing = true;
		multiple = false;
		vertices.push_back(pos);
		Editor &editor = getEditor();
		Graphics g(editor.getPreview());
		g.setColor(editor.getColor());
		g.drawPixel(pos);
//		pixelColor(editor.getPreview(), pos.first, pos.second, editor.getColor());
	}
}

void LinesTool::released(pair<int, int> pos, bool leftButton, bitset<5> state)
{
	if (vertices.size() < 1)
		return;
	if (drawing)
	{
		if (leftButton)
		{
			if (multiple)
			{
				vertices.push_back(pos);
			}
			else if (vertices.size() == 1 && vertices.front() == pos)
			{
				multiple = true;
			}
			else
			{
				vertices.push_back(pos);
				Editor &editor = getEditor();
				SDL_Rect changedArea = toSDL_Rect(vertices.front(), pos);
				SDL_FillRect(editor.getPreview(), &changedArea, 0x0);
				drawing = false;
				getPicture().addCommand(
						new DrawLinesCommand(vertices.begin(), vertices.end(), editor.getColor(),
								editor.getCurrentLayer()));
				vertices.clear();
			}
		}
		else if (multiple) //Right button and multiple
		{
			Editor &editor = getEditor();
			vertices.push_back(pos);
			SDL_Rect changedArea = largestSDL_Rect(vertices.begin(), vertices.end());
			SDL_FillRect(editor.getPreview(), &changedArea, 0x0);
			drawing = false;
			getPicture().addCommand(
					new DrawLinesCommand(vertices.begin(), vertices.end(), editor.getColor(),
							editor.getCurrentLayer()));
			vertices.clear();
		}
	}
}

void LinesTool::moved(pair<int, int> pos, pair<int, int> offset, std::bitset<5> state)
{
	if (drawing)
		if (multiple || state[BUTTON_LEFT])
		{
			Editor &editor = getEditor();

//			SDL_Rect changedArea = largestSDL_Rect(vertices.begin(), vertices.end());
			SDL_FillRect(editor.getPreview(), NULL, 0x0);
			pair<int, int> oldPos = vertices.back();
			Graphics g(editor.getPreview());
			g.setColor(editor.getColor());
			g.drawLine(oldPos, pos);
//			lineColor(editor.getPreview(), oldPos.first, oldPos.second, pos.first, pos.second, editor.getColor());
			if (multiple)
			{
				auto last = vertices.begin();
				for (auto it = last + 1; it != vertices.end(); ++it)
				{
					g.drawLine(*last, *it);
//					lineColor(editor.getPreview(), last->first, last->second, it->first, it->second, editor.getColor());
					last = it;
				}
			}
		}
//		vertices.push_back(pos);
//		for (size_t i = 0; i < vertices.size()-1; ++i) {
//			lineColor(editor.getPreview(), vertices[i].first, vertices[i].second, vertices[i+1].first, vertices[i+1].second, editor.getColor());
//		}
}

}

/* namespace yep */
